// *********************************************************
// Main.cpp- the main file that is run and loads everything. 
// it contains the main loop, which controls AI, UI, game dynamcis, etc

// Credit goes to: Nabeel Sneij for most of the code and PM, Alex Thacker for UI,
// Lisa Emering for models, Adam Schwartz for some code and PM
// *********************************************************



#include "DarkGDK.h"
#include "math.h"
//#include "PlaneEnv.cpp"
#include "global.h"
#include "intro.h" //loads all the constants and global variables
//#include "MusicPlayer.h"

	
#include "splash_screen.cpp"
#include "game_interface.cpp"
#include "economy.cpp"
//#include "Vehicle.cpp"

#include "classes.h"

void DarkGDK ( void )
{
	
	//make new music player
	MusicPlayer mPlayer;
	//dbSetWindowOff();

	//call up the selection screen when game loaded
	splash_screen selection_screen;

	//create new UI object
	game_interface game_ui; 
	
	//create new economy object
	economy money;

	//change display mode
	dbSetDisplayMode ( 800, 600, 32 );
	dbSetWindowPosition (0, 0 ); 
	
	// variables to get passed from interface
	int button_id;
	int civilization_number;
	int displayWait=0;
	
	//start the splash screen
	selection_screen.load_splash_images();

	//go into loop for splash screen
	while((civilization_number = selection_screen.show_splash()) == 0) {
		dbSync();
	}
	
	// set variable to avoid team related bugs. 
	player= 0;

	//loads everything
    #include "setup.h" 

	// creates the UI interface
	game_ui.create_interface();

	//create economy for player
	money.create_economy();

	//create LISA's BUILDING
	veh.NewVehicle(gold, 60);
	veh.NewVehicle(red, 62);
	
	//debug variable
	double moveVal=1;
	dbHideObject(1);

	//change mouse to regular pointer
	dbChangeMouse (0);		 

	// our main loop
	while ( LoopGDK ( ) )
	{

		//randomize timer
		dbRandomize(dbTimer());
		
		//mouse click function
		if (dbMouseClick()==1) mouseCounter=1; //triger the mouse Counter to start

		//useful for drag click, not fixed. 
		if (dbMouseClick()==0 && mouseCounter >0 &&  mouseCounter <3) { //real CLICK
			mouseClick=clicked;
			mouseCounter=0; //stop counter
		}

		//still clicked, therefore dragging
		if (dbMouseClick()==1 && mouseCounter >= 3) 
			mouseClick=dragged; 

		// once trigered, keep counting up
		if (mouseCounter > 0) mouseCounter++;
		

		// zoom of camera
		if (dbMouseClick()==1 && dbKeyState(44)==1 && dbCameraPositionY(1)>50) dbMoveCamera(1,5);
		if (dbMouseClick()==2 && dbKeyState(44)==1 && dbCameraPositionY(1)<200) dbMoveCamera(1,-5);
		

		//selector object of 3d objects- move screen as moving camera around
			if (dbObjectScreenX(1) < 0) 
				dbPositionObject(1,dbObjectPositionX(1)+5,0,
					dbObjectPositionZ(1));
			
			if (dbObjectScreenX(1) > dbScreenWidth()) dbPositionObject(1,dbObjectPositionX(1)-5,0,
					dbObjectPositionZ(1));
			
			if (dbObjectScreenY(1) < 0) dbPositionObject(1,dbObjectPositionX(1),0,
					dbObjectPositionZ(1)-5);

			if (dbObjectScreenY(1) > dbScreenHeight()) dbPositionObject(1,dbObjectPositionX(1),0,
					dbObjectPositionZ(1)+5);
			
			
			//move as mouse moves
			if (dbObjectInScreen(1)==1)
				dbPositionObject(1,dbObjectPositionX(1)+dbMouseMoveX()*moveVal,0,
					dbObjectPositionZ(1)-dbMouseMoveY()*moveVal);
			

			//position the mouse over the 3d object screen cordinates. 
			dbPositionMouse(dbObjectScreenX(1),dbObjectScreenY(1));
						
		
		
		//movement of camera of the selector object is moved passed screen 
		if (dbObjectScreenY(1) < 30) {
			dbPositionCamera ( dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ()+5);
			//dbPositionObject(1,dbObjectPositionX(1),0,dbObjectPositionZ(1)+4);

		}
		if (dbObjectScreenY(1) > dbScreenHeight()-30) {
			dbPositionCamera ( dbCameraPositionX(),dbCameraPositionY(),dbCameraPositionZ()-5);
			//dbPositionObject(1,dbObjectPositionX(1),0,dbObjectPositionZ(1)-4);
		}

		if (dbObjectScreenX(1) > dbScreenWidth()-30) {
			dbPositionCamera (dbCameraPositionX()+5,dbCameraPositionY(),dbCameraPositionZ());
			//dbPositionObject(1,dbObjectPositionX(1)+4,0,dbObjectPositionZ(1));
		}
		if (dbObjectScreenX(1) < 30) {
			dbPositionCamera (dbCameraPositionX()-5,dbCameraPositionY(),dbCameraPositionZ());
			//dbPositionObject(1,dbObjectPositionX(1)-4,0,dbObjectPositionZ(1));
		}

		//listen the the button return vals received from UI
		button_id = game_ui.interface_loop(civilization_number); 
		//money.display_money();
		//money.set_money(button_id);

		// CREATE NEW VEHICLES FROM INTERFRACE
		if (button_id > 0 && button_id  != 30 && money.money>0 && displayWait< 50)  {
				veh.NewVehicle(player, button_id);
				
				//for any values receivecd from interface, subject the proper amount from economy
				if (34 == button_id) money.set_money(money.money-50); 
				if (36 == button_id) money.set_money(money.money-200); 
				if (31 == button_id) money.set_money(money.money-100); 
				if (32 == button_id) money.set_money(money.money-400); 
				
				//holds the print statement of money deducted on screen
				displayWait=70;
		}
		//holds the print statement on screen
		if (displayWait>0) {
			dbPrint((double)button_id);
			dbPrint((double)displayWait);
			displayWait--;
			money.loop();
		}

		// randomly 1/10 chance give money to the economy
		if  (dbRND(10) == 1) money.set_money(money.money+5);
		
		// randomly 50/100, create new enemy units
		if  (dbRND(100) == 50) {
			veh.NewVehicle(red, 34);
			//who= veh.NewVehicle((player+1)%2, 54);
			//O[who][DX]= 0;
			//O[who][DZ]= 0;
		}





		//ALL OBJECTS LOOP - update the global array storing each value of each object
		for (int xx= 0; xx< objectLimit; xx++) {
			if (dbObjectExist(xx) ==1 && O[xx][L] > 0) {
					O[xx][X] = dbObjectPositionX(xx);
					O[xx][Z] = dbObjectPositionZ(xx);
					O[xx][AY] = dbObjectAngleY(xx);

					
			}

		}
		
		//flying airplanes behavior
			//tour planes
		for (int xx=tourPlaneStart; xx< tourPlaneEnd; xx++) {
		  dbMoveObject(xx,O[xx][SL]);
		  dbYRotateObject(xx,dbWrapValue(dbObjectAngleY(xx)+O[xx][RL]));
			
		  //if airplane went into the screen, make a sound. 
		  if (dbObjectInScreen(xx)==1) 
			 dbPlaySound(1002); 
		  //dbTurnObjectRight(xx,O[xx][RL]);
		}
		
		

		// bullet behavior
		for (int xx= bullNumStart; xx < bullNumEnd; xx++) {
			if (dbObjectExist(xx) ==1 && O[xx][L] >= 0 && O[xx][L] != 50) {
				dbMoveObject(xx,5);
				
				//for buildings, point bullets at enemeys
				if (O[xx][L] ==49) {
					int shooter = O[xx][OWN];
					if ( O[shooter] [MOVEABLE]==0) dbPointObject(xx, O[shooter] [DX],0, O[shooter] [DZ]);
				}

				//reduce life of bullet
				if (O[xx][L]<50) 
					O[xx][L] += -1;

				//kill the bullet
				if (O[xx][L]<0) {
					O[xx][L]=50;
					dbHideObject(xx); 
				}

				// hit something
				for (int yy= vehicleNumStart; yy < vehicleNumEnd; yy++) {
					if (dbObjectCollision(xx,yy)==1 && O[xx][OWN] != yy
						&& O[xx][L]>0) // as long as bullet is alive???
					{
						
						//do when hit
						if (O[yy][TM] != O[(int)O[xx][OWN]][TM] //not team's bullet
						&& O[yy][L]>0)  // as long as unit is alive???
						{
							//reduce life of unit
							O[yy][L] += -2;

							//play hit sound
							dbPlaySound(3);

							//make bullet dead
							O[xx][L] = 0;

							//say i'm i'm hit
							if (O[yy][TM] ==player) { //play "i'm hit" if player's tanks
								O[yy][HIT]=1;
								if (voiceSound == 1) {
									if (player== gold && offOn(3,10)==1) 
										dbPlaySound(dbRND(2)+ 49);
									if (player== red && offOn(3,10)==1) 
										dbPlaySound(dbRND(2)+ 91);
								}


								//play fight music
								mPlayer.play(1);


							// REVENGE if in your position
							if (dbSqrt( pow((O[yy][X] - O[yy][DX]),2)
								+ pow((O[yy][Z] - O[yy][DZ]),2) )<5
								&& O[yy][CM] == idle)
									O[yy][ET]=O[xx][OWN];

							//ally support
							for (int zz=vehicleNumStart; zz< vehicleNumEnd; zz++){
								if (O[zz][ET]==0//if has no enemy to attack
									&& O[zz][TM] == O[yy][TM] //on same team
									&& withinRange(zz,yy,100)==1  //within range
									&& O[zz][CM] == idle //not retreating
									&& O[zz][MOVEABLE]==1) { 
									
										O[zz][ET]=O[yy][ET]; //have the same enemy
									}
							}

							}
						}
						
						//heavy fire voice
						if (O[yy][L]<=4 && O[yy][TM]==player) {
							O[yy][DAM]=1;
							if (voiceSound==1) {
								if (player == gold && offOn(5,1)==1)
									dbPlaySound(dbRND(2)+ 22);
								if (player == red &&  offOn(5,1)==1)
									dbPlaySound(dbRND(2)+ 64);
							}
						}

						// if unit is dead, then decrease the count 
						if (O[yy][L]<=0 && O[yy][L]>-10) {
							if (O[yy][TM]==red)
								enemiesLeft--;

							if (O[yy][TM]==gold)
								alliesLeft--;

							O[yy][L]=-10; //to ensure this guy is dead once
							
							//play death sound
							dbPlaySound(9);
					
							dbDeleteObject(yy);
						}
					}
				}
			}	
		}

		
	
		//to determine if team has died
			//it says both team is dead now, but it goes thru the checklist, and if 
			//and if it finds just one unit alive, it trips the varibles to alive by
			// setting it = to 0. 
		goldDeath=1;
		redDeath=1;


		// UNIT BEHAVIOR
		for (int xx= vehicleNumStart; xx< vehicleNumEnd; xx++) { //REPLACE #s with variables
			
			//if still alive
			if (O[xx][L] > 0) {

				//here is the tripper code for alive teams
				if (O[xx][TM]==gold)
					goldDeath=0;
				if (O[xx][TM]==red)
					redDeath=0; 

				// get vehichle attention- select units for player
				if (dbMouseClick()==1
					&& dbObjectCollision(1,xx)==1
					&& dbKeyState(44)==0 
					&& O[xx][TM] == player
					) {

					O[xx][LS]=listening;
					multiSelect++;
					if (voiceSound == 1) {
						if (player == gold && offOn(2,10)==1) 
							dbPlaySound(dbRND(2)+ 40);
						if (player == red && offOn(2,10)==1) 
							dbPlaySound(dbRND(2)+ 82);
					}
				}

				//cicles around selected
				dbInk(dbRGB(255-(O[xx][L]*255/O[xx][ML]),O[xx][L]*255/O[xx][ML],0),0);
				if (O[xx][LS]== listening)
					dbCircle(dbObjectScreenX(xx),dbObjectScreenY(xx),25);
				dbInk(dbRGB(255,255,255),0);


				// stop vehicle firing and moving
				if (dbKeyState(45)==1
					&& O[xx][TM] == player
					&& O[xx][LS]==listening) {


					//voice
						if (voiceSound ==1) {
							if (player == gold && offOn(1,10)==1 && O[xx][ET]==0) 
								dbPlaySound(dbRND(2)+ 46);
							if (player == red && offOn(1,10)==1 && O[xx][ET]==0) 
								dbPlaySound(dbRND(2)+ 88);
						}

						//stop the unit?
					O[xx][CM]=stop; 
					
					
				}

				//stop EVERYTHING including retreat
				if (O[xx][CM]==stop) {

					O[xx][CM]=idle;
					O[xx][ET]=0;
					O[xx][FW]=0;

					O[xx][DX] = O[xx][X];
					O[xx][DZ] = O[xx][Z];
					
				}

				// stop and retreat
				if (dbKeyState(19)==1
					&& O[xx][TM] == player
					&& O[xx][LS]==listening) {

						//play sound of retreat
					if (voiceSound ==1) {
							if (player == gold && offOn(8,10)==1 && O[xx][ET]==0) 
								dbPlaySound(dbRND(2)+ 37);
							if (player == red && offOn(8,10)==1 && O[xx][ET]==0) 
								dbPlaySound(dbRND(2)+ 79);
					}

					//set commadn to retreat
					O[xx][CM]=retreat; 

					O[xx][DX] = 0 + dbRND(50)-25;
					O[xx][DZ] = 0 + dbRND(50)-25;

					if (O[xx][MOVEABLE]==1) dbPointObject(xx,O[xx][DX],0,O[xx][DZ]);

					O[xx][MOVM]=1;
					O[xx][ET]=0; //no enemy now
					
				}

				//if reached retreat point, attack back
				if (O[xx][CM]==retreat 
					&& dbSqrt( pow((O[xx][X] - O[xx][DX]),2)
					+ pow((O[xx][Z] - O[xx][DZ]),2) )< 10)
					O[xx][CM]=idle;


				//TEMP DRAG CODE=====================================
				//applied all commands to units, mouseClick is finished, turn it off
				if (multiSelCount ==0) //XXXXX
					mouseClick = idle; 

				//starts off with the amount selected
				multiSelCount = multiSelect; 
				
				//set destinations for player/ click to move
				if (dbMouseClick()==1     //  XXX// fix by saying mouseClick==clicked
					&& dbObjectCollision(1,0)==0
					&& O[xx][LS] == listening
					&& dbKeyState(44)==0
					) {

					multiSelCount--; //set it off to reset
					O[xx][CM]=move;
					O[xx][DX] = dbObjectPositionX(1) + dbRND(30)-15;
					O[xx][DZ] = dbObjectPositionZ(1) + dbRND(30)-15;


					// go slower to fire?
					if (O[xx][HIT]==0 && dbShiftKey()==0) {

						O[xx][MOVM]=0; //go slower to be able to fire

						if (voiceSound ==1) {
							if (player == gold && offOn(1,10)==1) 
								dbPlaySound(dbRND(2)+ 31);

							if (player == red && offOn(1,10)==1) 
								dbPlaySound(dbRND(2)+ 73);
						}

						
					}
					O[xx][ET]=0;
					if (O[xx][MOVEABLE]==1) dbPointObject(xx,O[xx][DX],0,O[xx][DZ]);

					//hurry up
					if (dbShiftKey()==1 && O[xx][HIT]==0) {
						O[xx][MOVM]=1; //go faster

						if (voiceSound ==1) {
							if (player == gold && offOn(1,10)==1) 
								dbPlaySound(dbRND(2)+ 28);
							if (player == red && offOn(1,10)==1) 
								dbPlaySound(dbRND(2)+ 70);
						}
					}
			
				}
			
				
				//attack selected for player
				if (dbMouseClick()==1
					&& O[xx][LS] == listening
					&& dbKeyState(44)==0) {
					
						//controls for individual units
					for (int yy= vehicleNumStart; yy< vehicleNumEnd; yy++) {
						if (dbObjectCollision(1,yy)==1
							&& O[yy][TM] != player
							&& O[yy][L]>=0) {

							
							if (O[xx][MOVEABLE]==1)dbPointObject(xx,O[yy][X],0,O[yy][Z]);
							O[xx][ET]=yy;
						
							//since can't fire weapon while speeding
							O[xx][MOVM]=idle;

							//far fire 
							if (dbKeyState(33)==1) {
								O[xx][CM]=ffire;
								O[xx][AR]=170;

								if (voiceSound ==1) {
									if (player == gold && offOn(7,20)==1) 
										dbPlaySound(dbRND(2)+ 16);
									if (player == red && offOn(7,20)==1) 
										dbPlaySound(dbRND(2)+ 58);
								}
							}
							else 

							if (dbKeyState(31)==1) {//near fire- shortrange
								O[xx][CM]=nfire;
								O[xx][AR]=60;

								//play sound
								if (voiceSound ==1) {
									if (player == gold && offOn(7,20)==1) 
										dbPlaySound(dbRND(2)+ 34);
									if (player == red && offOn(7,20)==1) 
										dbPlaySound(dbRND(2)+ 76);
								}
							}
							else 

								//hold position & fire 
								if (dbKeyState(32)==1) {
									O[xx][CM]=hfire;

								//play sound
								if (voiceSound ==1) {
									if (player == gold && offOn(7,20)==1) 
										dbPlaySound(dbRND(2)+ 25);
									if (player == red && offOn(7,20)==1) 
										dbPlaySound(dbRND(2)+ 67);
								}

							}
							else 
								{
								//normal attack mode
									O[xx][CM]=attack; //attack normal (may not be used)
									O[xx][AR]=100; //normal range

									if (voiceSound ==1) {
										if (player == gold && offOn(7,20)==1) 
											dbPlaySound(dbRND(2)+ 13);

										if (player == red && offOn(7,20)==1) 
											dbPlaySound(dbRND(2)+ 55);
									}
								}
						}
					}
				}

					//after enemy destroyed, stop moving
					int temp3= O[xx][ET]; 
					if (O[temp3][L] <= 0  && O[xx][ET] != 0) {
						O[xx][DX] = O[xx][X];
						O[xx][DZ] = O[xx][Z];
						 O[xx][ET] = 0;
					}
				

				//attack details
				if (O[xx][ET]>0) {
					
					int enemyID=O[xx][ET];
					O[xx][DX] = O[enemyID][X];  
					O[xx][DZ] = O[enemyID][Z];
					if (O[xx][MOVEABLE]==1) dbPointObject(xx,O[xx][DX],0,O[xx][DZ]); //NOT EFFECTIVE, OTHER CODE FIXES PROBLEM
					mPlayer.play(1);

					if (withinRange(xx,O[xx][ET],200)==1)
						O[xx][FW]=fire;
				}
				

				//machine gun attack with fake bullets (don't see them)
				if (withinRange(xx,(int)O[xx][ET],80)==1
					&& O[xx][TM]==player 
					&& O[xx][WR2] == 0
					&& O[(int)O[xx][ET]][L]>=0
					&& O[xx][ET] != 0) {

						O[xx][WR2] = 50;
						O[(int)O[xx][ET]][L] -= .4;
						dbPlaySound(dbRND(3)+ 200);	
				}

				if (O[xx][WR2] > 0) 
					O[xx][WR2] --;

				//move all objects to their destination 
				if (dbSqrt( pow((O[xx][X] - O[xx][DX]),2)
					+ pow((O[xx][Z] - O[xx][DZ]),2) )>5
					&& O[xx][ET]==0
					&& O[xx][MOVEABLE]==1) 
				{
					dbMoveObject(xx,.5);
					if (O[xx][MOVM]==1)
						dbMoveObject(xx,.35); //move faster
				}


				//move all objects to their ATTACK destination 
				if (dbSqrt( pow((O[xx][X] - O[xx][DX]),2)
					+ pow((O[xx][Z] - O[xx][DZ]),2) )> O[xx][AR]
					&& O[xx][ET]>0
					&& O[xx][CM] != hfire
					&& O[xx][MOVEABLE]==1) 
				{
					dbMoveObject(xx,.5);
					if (O[xx][MOVM]==1)
						dbMoveObject(xx,.35); //move faster
				}

				//move all objects to their ATTACK destination  BACKWARDS
				if (dbSqrt( pow((O[xx][X] - O[xx][DX]),2)
					+ pow((O[xx][Z] - O[xx][DZ]),2) )< O[xx][AR]-10
					&& O[xx][ET]>0
					&& O[xx][CM]==ffire
					&& O[xx][MOVEABLE]==1) //if far range 
				{
					dbMoveObject(xx,-.5);
				}

				////enemy spotted voice
				//if (O[xx][TM]==player) {
				//	int loopout=0;
				//	for (int yy= vehicleNumStart; yy< vehicleNumEnd && loopout==0; yy++)
				//		if (O[yy][TM] != player && O[yy][L]>=0) {//if yy is enemy
				//			if (withinRange(xx,yy,300)==1) 
				//				if (offOn(5,10000)==1 && O[xx][HIT]==0) {
				//					
				//					dbPlaySound(dbRND(2)+ 43);
				//					loopout=1;
				//				}
				//		}
				//	
				//}
				//


				//arrival voice & stop moving fast
				if (dbSqrt( pow((O[xx][X] - O[xx][DX]),2)
					+ pow((O[xx][Z] - O[xx][DZ]),2) )>5
					&& dbSqrt( pow((O[xx][X] - O[xx][DX]),2)
					+ pow((O[xx][Z] - O[xx][DZ]),2) )<10
					&& O[xx][TM]==player) {

						//stop moving 
						O[xx][MOVM]=0;

						if (voiceSound ==1) {
							//say arrived
							if (player== gold && offOn(1,20)==1 && O[xx][HIT]==0 && O[xx][CM] != stop) 
									dbPlaySound(dbRND(2)+ 10);		
							if (player== red && offOn(1,20)==1 && O[xx][HIT]==0 && O[xx][CM] != stop) 
									dbPlaySound(dbRND(2)+ 52);
						}
						
				}
			
				//under attack voice
				if (O[xx][HIT]>0) {
					O[xx][HIT]++;
					O[xx][MOVM]=0;
				}

				if (O[xx][HIT]>40) 
					O[xx][HIT]=0;
					
				//unselect objects
				if (dbMouseClick()==2 && O[xx][LS]==listening && dbKeyState(44)==0) {
					O[xx][LS]=idle;
					multiSelect=0;
				}

				//BASIC  AI:________________________________
				//BASIC  AI:________________________________
				//if (O[xx][TM] != player) 
					int loopout2=0; // to break out of loops
					for (int yy= vehicleNumStart; yy< vehicleNumEnd && loopout2==0; yy++) {
						if (O[xx][TM] != O[yy][TM]
						&& O[yy][L]>=0
						&& dbSqrt( pow((O[xx][X] - O[xx][DX]),2)
							+ pow((O[xx][Z] - O[xx][DZ]),2) )<5
						&& O[xx][ET]==0
						&& withinRange(xx,yy,200) ==1) {

							  //if ((withinRange(xx,yy,100) ==0)
								//  && (withinRange(xx,yy,150) ==1)
								//  && O[xx][ET]==0)

										//dbMoveObject(xx,.2);
									O[xx][ET]=yy;
										//dbPointObject(xx, O[yy][X],0,O[yy][Z]);

									//dbPointObject(xx, O[yy][X],0,O[yy][Z]);
									//O[xx][FW]=fire;

							  loopout2=1;
							  //else changeMusic(7);
						}	
					}

					// FW=3-fire
					int loopout=0; //special variable to break out of loop if we found the bullet
				     
					if (O[xx][FW]==fire && O[xx][MOVM]==0) {
						
						for (int yy=bullNumStart; yy< bullNumEnd; yy++) { //all the bullets
							
					  
							if (O[yy][L]==50 //if bullet is ready
								&& O[yy-1][L]<50 //if the previous bullet is already in use
								//&& O[xx][PR]>0 //if the 
								&& loopout==0 
								&& O[xx][WR]==0
								&& O[xx][L]>0) { //and weapon recoil is ready
								
								   //O[xx][PR] += -5; //dec the power
								   O[xx][WR] = 250; //create weapon recoil
								   dbPlaySound(4);

								   O[yy][L]=49; //bullet intialized to die
								   //BL#[yy][4]=O[xx][S] make bullet the same speed of vehicle

								   //BL#[yy][1]= S#[xx][NX2]; 
								   //BL#[yy][,2)= S#(xx,NZ2)
								   
								   //BL[yy][BULA]= O[xx][AY]; //point in same direction
									O[xx][AY]= dbObjectAngleY(xx); //update angle before

								   dbYRotateObject(yy,O[xx][AY] + dbRND(10) -5);
								   dbPositionObject(yy,O[xx][X],0,O[xx][Z]);

								   O[yy][OWN]= xx; //who's bullet is it. 
								   dbShowObject(yy);
								   loopout=1;
							}
						}
					}
				  loopout=0;
				
				  // power recoil recharge and weapon recoil
				  if (O[xx][WR]>0) 
					  O[xx][WR] += -1;
				  //if S#(xx,PR)<20 then inc S#(xx,PR),.2

				// fire for team player
				if (dbControlKey()==1 && O[xx][TM] == player && O[xx][MOVM]==0) //manually make vehicles fire
					O[xx][FW]=fire;

				else 
					O[xx][FW]=idle; 
				}
			
		}

		//loop music
		mPlayer.loop();

		//PLAY MUSIC
		if (goldDeath==1 && player == gold) mPlayer.play(2);
		if (goldDeath==1 && player == red) mPlayer.play(3);

		if (redDeath==1 && player == red) mPlayer.play(2);
		if (redDeath==1 && player == gold) mPlayer.play(3);
		
		// player lost code/change music
		if (mPlayer.getMode()==2) {
			while (dbEscapeKey()==0) {
				mPlayer.changeMusic(2);
				mPlayer.loop();
				dbCenterText(300,250,"You Lost Loser. Play again to lose again loser. Press escape to exit");
				dbSync();
			}
			
		}

		// player won code
		if (mPlayer.getMode()==3) {		
			while (dbEscapeKey()==0) {
				mPlayer.changeMusic(3);
				mPlayer.loop();
				dbCenterText(300,250,"You should have lost loser, but you won. Press escape to exit");
				dbSync();
			}
		}

		//in case camera or mouse gets stuck, have h key. 
		if (dbKeyState(35)==1) { //cam go back home
			dbPositionObject(1,0,0,0); 
			dbPositionCamera(0,100,0); 

		}

		//display all player's controls
		dbSetCursor(0,00);

		//dbPrint((float)dbScanCode());
		dbPrint("Metal vs Magic");
		dbPrint("Objective: attack all enemy units North East of your view.");
		if (player == gold) dbPrint("-------------Your team: Metal Warriors------------");
		else dbPrint("-------------Your team: Magic Warriors------------");
		dbPrint("Metal Warriors Left: ");
		dbPrint((float)enemiesLeft);
		dbPrint();

		dbPrint("Magic Warriors Left: ");	
		dbPrint((float)alliesLeft);
		dbPrint();	
		dbPrint("Press [q] for help and [Esc] to exit.");
		dbPrint("Don't switch windows, there is a bug. Use alt-tab to switch back here.");
		dbPrint("Press [h] if screen or mouse gets stuck.");
		
		dbPrint("Money: -------");
		dbPrint((double) money.money);

		//print out the extra options
		if (dbKeyState(16)==1) {
			dbPrint();
			dbPrint();
			dbPrint();
			dbPrint();
			
			dbPrint();
			dbPrint("Keys");
			dbPrint("Left click to select multiple units");
			dbPrint("Right click to clear selection");
			dbPrint("Press [x] to stop whatever unit is doing");
			dbPrint("Press [s] + click on enemy to shoot machine gun in close range");
			dbPrint("Press [d] + click on enemy to shoot from position");
			dbPrint("Press [f] + click on enemy to fire from far/long range");
			dbPrint("Press [r] to to retreat to orgin");
			dbPrint("Press [Ctrl] to have all units force fire");
			dbPrint();
			dbPrint("Press [z] + left or right mouse button to zoom");
			dbPrint("Press [h] to return to origin (also to fix camera's bad behavior)");
			dbPrint("Objectives: search and destroy all enemy units");
			dbPrint("(Hint: enemy is in top right corner)");
			
		}
		// update the screen
		dbSync ( );
	}
		//delete all objects to go back to windows
		for ( int i = 1; i < 200; i++ )
			dbDeleteObject ( i );

		for ( int i = 1; i < 20; i++ )
			dbDeleteSprite ( i );

		for ( int i = 1; i < 20; i++ )
			dbDeleteSound ( i );

		for ( int i = 30000; i < 30100; i++ )
			dbDeleteObject ( i );
	
	
	// return back to windows
	return;
}